iT邦幫忙

2022 iThome 鐵人賽

DAY 30
0
Modern Web

clojure 刷刷鍋系列 第 30

Clojure 肉片 -第 30 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

(= (array-diff [1 2] [1]) [2])
If a value is present in b, all of its occurrences must be removed from the other:

(= (array-diff [1,2,2,2,3] [2]) [1,3])

(必須通過以下測試)

(ns array-diff-test
  (:require [clojure.test :refer :all]
            [array-diff :refer :all]))

(deftest example-tests
  (is (= (array-diff [1 2] [1]) [2]))
  (is (= (array-diff [1 2 2] [1]) [2 2]))
  (is (= (array-diff [1 2 2] [2]) [1]))
  (is (= (array-diff [1 2 2] []) [1 2 2]))
  (is (= (array-diff [1 2 3] [1 2]) [3]))
  (is (= (array-diff [] [1 2]) [])))

【我的答案】

(ns array-diff)
(defn array-diff [a b]
  (remove (set b) (seq a))
)

【其他人的答案】

(ns array-diff)

(defn array-diff [a b]
  (remove (set b) a))
(ns array-diff)
(defn array-diff [a b]
  (filter #(= -1 (.indexOf b %) ) a)
)

上一篇
Clojure 肉片 -第 29 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言